﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormTaiSanCapNhap : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormTaiSanCapNhap()
        {
            InitializeComponent();
        }
        public int id = 0;
        
        private void bttLuu_Click(object sender, EventArgs e)
        {
            Edit();
        }
        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void KiemTraDuLieu()
        {
            if (txtTenTaiSan.Text != "" && txtThoiGianKhauHao.Text != "" && Convert.ToInt32(cbbNhomTaiSan.SelectedValue)>0 && txtTiLeKhauHao.Text!="")
            {
                bttLuu.Enabled = true;
            }
            else
            {
                bttLuu.Enabled = false;
            }
        }

        private void FormTaiSanCapNhap_Load(object sender, EventArgs e)
        {
            cbbNhomTaiSan.DataSource = db.NhomTaiSans.ToList();
            cbbNhomTaiSan.DisplayMember = "TenNhomTaiSan";
            cbbNhomTaiSan.ValueMember = "Id";
            cbbDonViTinh.DataSource = db.DonViTinhs.ToList();
            cbbDonViTinh.DisplayMember = "TenDonViTinh";
            cbbDonViTinh.ValueMember = "Id";
            if (id!=0)
            {
                var t = db.TaiSans.Find(id);
                txtTenTaiSan.Text = t.TenTaiSan;
                cbbNhomTaiSan.SelectedValue = t.NhomTaiSanId;
                cbbDonViTinh.SelectedValue = t.DonViTinhId;
                txtNamSanXuat.Text = t.NamSanXuat;
                if (t.ThoiGianBatDauSuDung != null) dtiThoiGianBatDauSuDung.Value = (DateTime)t.ThoiGianBatDauSuDung;
                txtThoiGianKhauHao.Text = t.ThoiGianKhauHao.ToString();
                txtTiLeKhauHao.Text = t.TiLeKhauHao.ToString();
                txtDienGiai.Text = t.DienGiai;
            }
            else
            {
                MessageBox.Show("Không tải được dữ liệu!");
                this.Close();
            }
        }

        void Edit()
        {
            try
            {
                var t = db.TaiSans.Find(id);

                t.TenTaiSan = txtTenTaiSan.Text;
                t.NhomTaiSanId = Convert.ToInt32(cbbNhomTaiSan.SelectedValue);
                t.DonViTinhId = Convert.ToInt32(cbbDonViTinh.SelectedValue);
                t.NamSanXuat = txtNamSanXuat.Text;

                t.ThoiGianBatDauSuDung = dtiThoiGianBatDauSuDung.Value;
                t.ThoiGianKhauHao = Convert.ToInt32(txtThoiGianKhauHao.Text);
                t.GiaTriKhauHao = t.GiaTriKhauHao;
                t.TiLeKhauHao = t.TiLeKhauHao;

                t.DienGiai = txtDienGiai.Text;
                t.NgayTinhKhauHao = t.NgayTinhKhauHao;

                t.TrangThai = t.TrangThai;

                db.Entry(t).State = EntityState.Modified;
                db.SaveChanges();
                this.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            
        }

        private void txtTenTaiSan_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void txtThoiGianTinhKhauHao_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void cbbNhomTaiSan_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void txtTiLeKhauHao_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void cbbDonViTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void txtGiaTriKhauHao_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void txtNamSanXuat_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void txtThoiGianKhauHao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtNamSanXuat_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txtTiLeKhauHao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
        
    }
}
